package shzj.web.controller;

import StaticFinal.CrudType;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import services.company.CompanyInfoServices;
import services.dictionary.CertificateTypeServices;
import services.dictionary.ItemTypeServices;
import services.dictionary.MajorTypeServices;
import services.expertInfo.*;
import services.staff.StaffInfoServices;
import services.upms.AccountPermissionServices;
import services.upms.AccountServices;
import services.upms.PermissionRoleServices;
import services.upms.RoleServices;
import shzj.orm.dao.*;
import shzj.orm.entity.*;
import util.JsonUtil;
import wt.entity.company.CompanyInfo;
import wt.entity.dictionary.CertificateType;
import wt.entity.dictionary.ItemType;
import wt.entity.dictionary.MajorType;
import wt.entity.expertInfo.*;
import wt.entity.staff.StaffInfo;
import wt.entity.upms.Account;
import wt.entity.upms.AccountPermission;
import wt.entity.upms.PermissionRole;
import wt.entity.upms.Role;
import wt.staticData.ShortData;

import java.util.*;

/**
 * Created by Administrator on 2018/1/15.
 */
@Controller
public class indexController {
    @Autowired
    ItemTypeServices itemTypeServices;
    @Autowired
    AccountServices accountServices;
    @Autowired
    RoleServices roleServices;
    @Autowired
    CompanyInfoServices companyInfoServices;
    @Autowired
    StaffInfoServices staffInfoServices;
    @Autowired
    ExpertBaseServices expertBaseServices;
    @Autowired
    ExpertCertificateServices expertCertificateServices;
    @Autowired
    ExpertCompanyServices expertCompanyServices;
    @Autowired
    ExpertTrainServices expertTrainServices;
    @Autowired
    ExpertReviewServices expertReviewServices;
    @Autowired
    ExpertWorkServices expertWorkServices;
    @Autowired
    ExpertEducationServices expertEducationServices;
    @Autowired
    MajorTypeServices majorTypeServices;
    @Autowired
    ExpertMajorServices expertMajorServices;
    @Autowired
    CertificateTypeServices certificateTypeServices;
    @Autowired
    PermissionRoleServices permissionRoleServices;
    @Autowired
    AccountPermissionServices accountPermissionServices;


    @Autowired
    Account1Mapper account1Mapper;
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    CertificateType1Mapper certificateType1Mapper;
    @Autowired
    CompanyInfo1Mapper companyInfo1Mapper;
    @Autowired
    EducationExperienceMapper educationExperienceMapper;
    @Autowired
    ExpertCertificate1Mapper expertCertificate1Mapper;
    @Autowired
    ExpertInfoMapper expertInfoMapper;
    @Autowired
    ProfessionExpertMapper professionExpertMapper;
    @Autowired
    ProfessionSetMapper professionSetMapper;
    @Autowired
    ReviewExperienceMapper reviewExperienceMapper;
    @Autowired
    Role1Mapper role1Mapper;
    @Autowired
    SelectSetMapper selectSetMapper;
    @Autowired
    StaffInfo1Mapper staffInfo1Mapper;
    @Autowired
    TrainExperienceMapper trainExperienceMapper;
    @Autowired
    WorkCompanyMapper workCompanyMapper;
    @Autowired
    WorkExperienceMapper workExperienceMapper;
    @Autowired
    CategoryExpertGradeMapper categoryExpertGradeMapper;


    @RequestMapping("typeList")
    public String typeList(@RequestParam(value = "typeList[]") Set<String> typeList) throws Exception {

        // 事项
        if (typeList.contains("itemType")) {
            List<Category> categoryList = categoryMapper.selectByExample(null);
            itemTypeServices.deleteByExample(null);
            for (Category v : categoryList) {
                ItemType temp = new ItemType();
                temp.setId(pkToId(v.getPk()));
                temp.setLive(v.getLive().shortValue());
                temp.setName(v.getName());
                temp.setNum(v.getNum());
                temp.setSeries(v.getSeries().intValue());
                itemTypeServices.insertSelective(temp);
            }
        }
        //证书类型
        if (typeList.contains("certificateType")) {
            List<CertificateType1> categoryList = certificateType1Mapper.selectByExample(null);
            certificateTypeServices.deleteByExample(null);
            for (CertificateType1 v : categoryList) {
                CertificateType temp = new CertificateType();
                temp.setId(pkToId(v.getPk()));
                temp.setItemId(pkToId(v.getCategoryFk()));
                temp.setName(v.getName());
                temp.setRemark(v.getRemark());
                temp.setLive(v.getLive().shortValue());
                certificateTypeServices.insertSelective(temp);
            }
        }

        // 专业类型
        if (typeList.contains("majorType")) {
            ProfessionSetExample professionSetExample = new ProfessionSetExample();
            professionSetExample.setOrderByClause("pk");
            List<ProfessionSet> professionSetList = professionSetMapper.selectByExample(professionSetExample);

            Long pk = null;
            for (int i = 0; i < professionSetList.size(); i++) {
                ProfessionSet set = professionSetList.get(i);
                if (set.getSelfPk() == null) {
                    pk = set.getPk().longValue();
                    professionSetList.remove(i);
                    break;
                }
            }

            for (int i = 0; i < professionSetList.size(); i++) {
                ProfessionSet set = professionSetList.get(i);
                if (pk.equals(set.getSelfPk())) {
                    set.setSelfPk(null);
                }
            }

            List<ItemType> itemTypeList = itemTypeServices.selectByExample(null);
            Map<String, String> itemTypeMap = new HashMap();
            for (ItemType itemType : itemTypeList) {
                itemTypeMap.put(itemType.getNum(), itemType.getId());
            }

            majorTypeServices.deleteByExample(null);
            for (ProfessionSet v : professionSetList) {
                MajorType temp = new MajorType();
                temp.setId(pkToId(v.getPk()));
                temp.setPid(pkToId(v.getSelfPk()));
                temp.setItemId(itemTypeMap.get(v.getNum().substring(0, 2)));
                temp.setNum(v.getNum());
                temp.setName(v.getExtend3());
                temp.setMaxDepth((v.getSeries() == null ? 0 : v.getSeries().intValue()) + 1);
                temp.setCurrentDepth(v.getTreeDepth().intValue() - 1);
                temp.setOrders(v.getPk().longValue());
                temp.setLive(v.getLive().shortValue());
                majorTypeServices.insertSelective(temp);
            }
        }

        if (typeList.contains("expertBase")) {
            List<ExpertInfo> expertInfoList = expertInfoMapper.selectByExample(null);
            expertBaseServices.deleteByExample(null);
            for (ExpertInfo v : expertInfoList) {
                ExpertBase temp = new ExpertBase();
                temp.setId(pkToId(v.getPk()));
                temp.setName(v.getName());
                temp.setIdCard(v.getIdCard());
                temp.setBirthday(v.getBirthday());
                if ("男".equals(v.getSex())) {
                    temp.setSex(ShortData.s1);
                } else if ("女".equals(v.getSex())) {
                    temp.setSex(ShortData.s0);
                }
                temp.setPhone(v.getPhone());
                temp.setEmail(v.getEmail());
                temp.setBankCard(v.getBankCard());
                temp.setBankName(v.getBankName());
                temp.setHeadImg(v.getHeadImg());

                if ("false".equals(v.getRetire())) {
                    temp.setRetire(ShortData.s0);
                } else if ("true".equals(v.getRetire())) {
                    temp.setRetire(ShortData.s1);
                }

                if ("false".equals(v.getTeacher())) {
                    temp.setTeacher(ShortData.s0);
                } else if ("true".equals(v.getTeacher())) {
                    temp.setTeacher(ShortData.s1);
                }


                if ("false".equals(v.getInout())) {
                    temp.setOutLander(ShortData.s0);
                } else if ("true".equals(v.getInout())) {
                    temp.setOutLander(ShortData.s1);
                }

                if ("true".equals(v.getLive())) {  //不能为空
                    temp.setLive(ShortData.s1);
                } else {
                    temp.setLive(ShortData.s0);
                }
                temp.setEducation(EDUCATION_MAP.get(v.getEducation()));
                temp.setRank(RANK_MAP.get(v.getRank()));
                temp.setCrudType(CrudType.select);
                expertBaseServices.insertSelective(temp);
            }
        }

        if (typeList.contains("expertCompany")) {

            List<WorkCompany> workCompanyList = workCompanyMapper.selectByExample(null);
            expertCompanyServices.deleteByExample(null);
            for (WorkCompany v : workCompanyList) {
                ExpertCompany temp = new ExpertCompany();
                temp.setId(pkToId(v.getPk()));
                temp.setExpertId(pkToId(v.getExpertFk()));
                temp.setName(v.getName());
                temp.setCode(v.getId());
                temp.setProperty(COMPANY_PROPERTY_MAP.get(v.getProperty()));
                temp.setAddress(v.getAddress());
                temp.setPhone(v.getPhone());
                temp.setAffiliate(v.getAffiliate());
                temp.setAffiliateCode(v.getAffiliateId());
                temp.setCrudType(CrudType.select);
                expertCompanyServices.insertSelective(temp);
            }
        }


        if (typeList.contains("expertCertificate")) {
            List<CertificateType1> certificateType1List = certificateType1Mapper.selectByExample(null);
            Map<Long, Long> stringMap = new HashMap<Long, Long>();
            for (CertificateType1 certificateType1 : certificateType1List) {
                stringMap.put(certificateType1.getPk(), certificateType1.getCategoryFk());
            }

            List<CategoryExpertGrade> categoryExpertGradeList = categoryExpertGradeMapper.selectByExample(null);
            Map<String, String> expertGrade = new HashMap<String, String>();
            for (CategoryExpertGrade categoryExpertGrade : categoryExpertGradeList) {
                expertGrade.put(categoryExpertGrade.getExpertFk() + "#" + categoryExpertGrade.getCategoryFk(), categoryExpertGrade.getGrade());
            }

            List<ExpertCertificate1> expertCertificateList = expertCertificate1Mapper.selectByExample(null);
            expertCertificateServices.deleteByExample(null);
            for (ExpertCertificate1 v : expertCertificateList) {
                ExpertCertificate temp = new ExpertCertificate();
                temp.setId(pkToId(v.getPk()));
                temp.setExpertId(pkToId(v.getExpertFk()));
                temp.setName(v.getName());
                temp.setCertificateTypeId(pkToId(v.getCertificateFk()));
                temp.setItemId(pkToId(stringMap.get(v.getCertificateFk())));
                temp.setGrade(expertGrade.get(v.getExpertFk() + "#" + stringMap.get(v.getCertificateFk())));
                if ("无".equals(temp.getGrade()) || StringUtils.isBlank(temp.getGrade())) {
                    temp.setGrade("C");
                }
                temp.setNum(v.getNum());
                temp.setEndTime(v.getEndTime()==null?new Date():v.getEndTime());
                if (v.getStartTime()==null){
                    System.out.println("dd");
                    
                }
                
                temp.setStartTime(v.getStartTime()==null?temp.getEndTime():v.getStartTime());
                temp.setImgSrc(v.getImgSrc());
                temp.setCrudType(CrudType.select);
                expertCertificateServices.insertSelective(temp);
            }
        }
        if (typeList.contains("expertMajor")) {
            List<ProfessionExpert> professionExpertList = professionExpertMapper.selectByExample(null);
            expertMajorServices.deleteByExample(null);
            for (ProfessionExpert v : professionExpertList) {
                ExpertMajor temp = new ExpertMajor();
                temp.setId(pkToId(v.getPk()));
                temp.setExpertId(pkToId(v.getExpertFk()));
                temp.setMajorId(pkToId(v.getClassificationFk()));
                temp.setCrudType(CrudType.select);
                try {
                    expertMajorServices.insertSelective(temp);
                } catch (Exception e) {
                    System.out.println(JsonUtil.obj2Json(temp));
                    throw  e;
                }


            }
        }
        if (typeList.contains("expertEducation")) {
            List<EducationExperience> educationExperienceList = educationExperienceMapper.selectByExample(null);
            expertEducationServices.deleteByExample(null);
            for (EducationExperience v : educationExperienceList) {
                ExpertEducation temp = new ExpertEducation();
                temp.setId(pkToId(v.getPk()));
                temp.setExpertId(pkToId(v.getExpertFk()));
                temp.setSchool(v.getSchool());
                temp.setMajor(v.getMajor());
                temp.setLearnTime(v.getLearnTime());
                temp.setCrudType(CrudType.select);
                expertEducationServices.insertSelective(temp);
            }
        }
        if (typeList.contains("expertReview")) {
            List<ReviewExperience> reviewExperienceList = reviewExperienceMapper.selectByExample(null);
            expertReviewServices.deleteByExample(null);
            for (ReviewExperience v : reviewExperienceList) {
                ExpertReview temp = new ExpertReview();
                temp.setId(pkToId(v.getPk()));
                temp.setExpertId(pkToId(v.getExpertFk()));
                temp.setCompany(v.getCompany());
                temp.setStartTime(v.getStartTime());
                temp.setEndTime(v.getEndTime());
                temp.setProduct(v.getProduct());

                if ("组长".equals(v.getRole())) {
                    temp.setRole(ShortData.s1);
                } else if ("组员".equals(v.getRole())) {
                    temp.setRole(ShortData.s0);
                }

                temp.setCrudType(CrudType.select);
                expertReviewServices.insertSelective(temp);
            }
        }


        if (typeList.contains("expertTrain")) {
            List<TrainExperience> trainExperienceList = trainExperienceMapper.selectByExample(null);
            expertTrainServices.deleteByExample(null);
            for (TrainExperience v : trainExperienceList) {
                ExpertTrain temp = new ExpertTrain();
                temp.setId(pkToId(v.getPk()));
                temp.setExpertId(pkToId(v.getExpertFk()));
                temp.setName(v.getTrainName());
                temp.setAddress(v.getAddress());
                if ("到".equals(v.getSignState())) {
                    temp.setSignState(ShortData.s1);
                } else if ("未到".equals(v.getSignState())) {
                    temp.setSignState(ShortData.s0);
                    temp.setReason(v.getSelectFk() + "");
                }


                temp.setStartTime(v.getStartTime());
                temp.setEndTime(v.getEndTime());
                temp.setCrudType(CrudType.select);
                expertTrainServices.insertSelective(temp);
            }
        }

        if (typeList.contains("expertWork")) {
            List<WorkExperience> reviewExperienceList = workExperienceMapper.selectByExample(null);
            expertWorkServices.deleteByExample(null);
            for (WorkExperience v : reviewExperienceList) {
                ExpertWork temp = new ExpertWork();
                temp.setId(pkToId(v.getPk()));
                temp.setExpertId(pkToId(v.getExpertFk()));
                temp.setName(v.getName());
                temp.setContent(v.getContent());
                temp.setRank(RANK_MAP.get(v.getRank()));
                temp.setStartTime(v.getStartTime());
                temp.setEndTime(v.getEndTime());
                temp.setCrudType(CrudType.select);
                expertWorkServices.insertSelective(temp);
            }
        }

        if (typeList.contains("staffInfo")) {
            List<StaffInfo1> staffInfoList = staffInfo1Mapper.selectByExample(null);
            staffInfoServices.deleteByExample(null);
            for (StaffInfo1 v : staffInfoList) {
                StaffInfo temp = new StaffInfo();
                temp.setId(pkToId(v.getPk()));
                temp.setUserId(v.getUserId());
                temp.setName(v.getName());
                temp.setIdCard(v.getIdCard());
                staffInfoServices.insertSelective(temp);
            }
        }


        if (typeList.contains("companyInfo")) {
            List<CompanyInfo1> companyInfoList = companyInfo1Mapper.selectByExample(null);
            companyInfoServices.deleteByExample(null);
            for (CompanyInfo1 v : companyInfoList) {
                CompanyInfo temp = new wt.entity.company.CompanyInfo();
                temp.setId(pkToId(v.getPk()));
                temp.setCompanyCode(v.getCompanyCode());
                temp.setName(v.getName());
                companyInfoServices.insertSelective(temp);
            }
        }

        if (typeList.contains("role")) {
            List<Role1> roleList = role1Mapper.selectByExample(null);
            permissionRoleServices.deleteByExample(null);
            roleServices.deleteByExample(null);
            for (Role1 v : roleList) {
                Role temp = new wt.entity.upms.Role();
                temp.setId(pkToId(v.getPk()));
                temp.setName(v.getName());
                temp.setRemark(v.getRemark());
                temp.setLive(ShortData.s1);
                temp.setCreateTime(new Date());
                roleServices.insertSelective(temp);
            }
        }

        if (typeList.contains("account")) {
            List<Account1> accountList = account1Mapper.selectByExample(null);
            List<PermissionRole> permissionRoleList = permissionRoleServices.selectByExample(null);
            Map<String, List<PermissionRole>> listMap = new HashMap<String, List<PermissionRole>>();
            for (PermissionRole permissionRole : permissionRoleList) {
                if (listMap.containsKey(permissionRole.getRoleId())) {
                    listMap.get(permissionRole.getRoleId()).add(permissionRole);
                } else {
                    List<PermissionRole> pr = new ArrayList<PermissionRole>();
                    pr.add(permissionRole);
                    listMap.put(permissionRole.getRoleId(), pr);
                }
            }
            accountPermissionServices.deleteByExample(null);
            accountServices.deleteByExample(null);
            for (Account1 v : accountList) {
                Account temp = new wt.entity.upms.Account();
                temp.setId(pkToId(v.getPk()));
                temp.setLoginName(v.getAccount());
                temp.setPassword(v.getPassword());
                temp.setRoleId(pkToId(v.getRoleFk()));
                temp.setExpertId(pkToId(v.getExpertFk()));
                temp.setCompanyId(pkToId(v.getCompanyFk()));
                temp.setStaffId(pkToId(v.getStaffFk()));
                temp.setLive(ShortData.s1);
                temp.setCreateTime(new Date());
                accountServices.insertSelective(temp);

                if (listMap.containsKey(temp.getRoleId())) {

                    for (PermissionRole permissionRole : listMap.get(temp.getRoleId())) {
                        AccountPermission accountPermission = new AccountPermission();
                        accountPermission.setAccountId(temp.getId());
                        accountPermission.setPermissionId(permissionRole.getPermissionId());
                        accountPermission.setType(ShortData.s1);
                        accountPermissionServices.insertSelective(accountPermission);
                    }
                }
            }
        }
        return "success";
    }

    private String pkToId(Long pk) {
        if (pk != null) {
            return (pk + 369000000000000000L) + "";
        } else {
            return null;
        }
    }


    public static final Map<String, Short> EDUCATION_MAP = new HashMap();//最高学历
    public static final Map<String, Short> RANK_MAP = new HashMap();//职称
    public static final Map<String, Short> COMPANY_PROPERTY_MAP = new HashMap();

    static {
        // education: {0: "博士", 1: "博士研究生", 2: "硕士", 3: "硕士研究生", 4: "本科", 5: "大专", 6: "中专", 7: "高中"},
        EDUCATION_MAP.put("博士", ShortData.s0);
        EDUCATION_MAP.put("博士研究生", ShortData.s1);
        EDUCATION_MAP.put("硕士", ShortData.s2);
        EDUCATION_MAP.put("硕士研究生", ShortData.s3);
        EDUCATION_MAP.put("本科", ShortData.s4);
        EDUCATION_MAP.put("大专", ShortData.s5);
        EDUCATION_MAP.put("中专", ShortData.s6);
        EDUCATION_MAP.put("高中", ShortData.s7);
        // rank: {0: "正高", 1: "副高", 2: "中级", 3: "其他"},
        RANK_MAP.put("正高", ShortData.s0);
        RANK_MAP.put("副高", ShortData.s1);
        RANK_MAP.put("中级", ShortData.s2);
        RANK_MAP.put("其他", ShortData.s3);
        //  companyProperty: {0: "企业", 1: "机关", 2: "事业单位", 3: "社会团体", 4: "其他"},
        COMPANY_PROPERTY_MAP.put("企业", ShortData.s0);
        COMPANY_PROPERTY_MAP.put("机关", ShortData.s1);
        COMPANY_PROPERTY_MAP.put("事业单位", ShortData.s2);
        COMPANY_PROPERTY_MAP.put("社会团体", ShortData.s3);
        COMPANY_PROPERTY_MAP.put("其他", ShortData.s4);
    }

}
